<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>table中溢出文本显示&#39;...&#39; | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="今天工作中遇到了一个需求，大致上是超出边框的文本隐藏，并在后面显示“…”   需求倒是没什么问题，也算比较常见了，理论上直接添加css样式就能解决。 12345.card-describe &amp;#123;    overflow: hidden; &#x2F;&#x2F; 超出隐藏    white-space: nowrap; &#x2F;&#x2F; 文字不换行    text-overflow">
<meta property="og:type" content="article">
<meta property="og:title" content="table中溢出文本显示&#39;...&#39;">
<meta property="og:url" content="http://yoursite.com/2020/09/04/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="今天工作中遇到了一个需求，大致上是超出边框的文本隐藏，并在后面显示“…”   需求倒是没什么问题，也算比较常见了，理论上直接添加css样式就能解决。 12345.card-describe &amp;#123;    overflow: hidden; &#x2F;&#x2F; 超出隐藏    white-space: nowrap; &#x2F;&#x2F; 文字不换行    text-overflow">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://yoursite.com/2020/09/04/img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA">
<meta property="og:image" content="http://yoursite.com/2020/09/04/img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA">
<meta property="og:image" content="http://yoursite.com/2020/09/04/img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA">
<meta property="og:image" content="http://yoursite.com/2020/09/04/img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA">
<meta property="og:image" content="http://yoursite.com/2020/09/04/img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA">
<meta property="article:published_time" content="2020-09-04T08:58:58.000Z">
<meta property="article:modified_time" content="2020-09-07T00:48:22.307Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yoursite.com/2020/09/04/img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-table中溢出文本显示&#39;...&#39;" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/09/04/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/" class="article-date">
  <time datetime="2020-09-04T08:58:58.000Z" itemprop="datePublished">2020-09-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      table中溢出文本显示&#39;...&#39;
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>今天工作中遇到了一个需求，大致上是超出边框的文本隐藏，并在后面显示“…”  </p>
<p>需求倒是没什么问题，也算比较常见了，理论上直接添加css样式就能解决。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.card-describe &#123;</span><br><span class="line">    overflow: hidden; &#x2F;&#x2F; 超出隐藏</span><br><span class="line">    white-space: nowrap; &#x2F;&#x2F; 文字不换行</span><br><span class="line">    text-overflow: ellipsis; &#x2F;&#x2F; 文字溢出显示...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>没想到的是，保存完以后竟然没有生效，嘛，仔细排查一下好了。  </p>
<p><img src="../img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/1.png" alt="图片"></p>
<p>开始以为是css出现了冲突，我写的样式被覆盖或者没有生效，为了验证一下，就把这几个css语句直接写进元素的行内style，结果发现并不是这个问题，我写的这三行样式确确实实生效了。  </p>
<p><img src="../img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/2.png" alt="图片"></p>
<p>这就奇怪了，这种情况下解决问题的最好方法自然是百度（百度打钱谢谢）  </p>
<p>我一查，好家伙，确实有人也遇到过这个问题，他们是发现在table样式中，text-overflow: ellipsis;失效。我这里用的ui框架是element ui，仔细排查一看，果然那个元素的默认style中有display: table;，剩下的就好办了，额外添加一句table-layout: fixed;即可修复这个问题。</p>
<p><img src="../img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/3.png" alt="图片"></p>
<p><img src="../img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/4.png" alt="图片"></p>
<p>解决bug之后，自然想要去了解了一下出现bug的根本原因。  </p>
<p>发现table-layout可以取这样几个值</p>
<p><img src="../img/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/5.png" alt="图片"></p>
<p>可以看出这个属性是改变了表格列宽度的设定方式，故据此推测，text-overflow属性在table模式下，是设定超出规定列宽（也因此需要设定width属性）以外的文字的展示方式，而元素在该模式下，默认列宽读取单元格内容的宽度，因此虽然设定了width为200px，但只是给表格宽度做了限制，并没有影响到列宽，text-overflow属性也就此失效。  </p>
<p>而在设定了table-layout: fixed;后，列宽改为读取表格宽度，即读取到了我设定好的width: 200px;属性，此时text-overflow属性才能确定有文本超出了列宽，进而对超出的内容进行处理。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/09/04/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/" data-id="ckeskugoq00018kvph2yi7ep9" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/09/07/js%E9%87%8CforEach%E9%81%8D%E5%8E%86%E4%B8%AD%E4%BD%BF%E7%94%A8continue%E6%8A%A5%E9%94%99/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          js里forEach遍历中使用continue报错
        
      </div>
    </a>
  
  
    <a href="/2020/08/03/vue-router%E9%BB%98%E8%AE%A4%E6%98%BE%E7%A4%BA%E5%AD%90%E8%B7%AF%E7%94%B1/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">vue-router默认显示子路由</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/09/">September 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/07/">July 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/09/07/js%E9%87%8CforEach%E9%81%8D%E5%8E%86%E4%B8%AD%E4%BD%BF%E7%94%A8continue%E6%8A%A5%E9%94%99/">js里forEach遍历中使用continue报错</a>
          </li>
        
          <li>
            <a href="/2020/09/04/table%E4%B8%AD%E6%BA%A2%E5%87%BA%E6%96%87%E6%9C%AC%E6%98%BE%E7%A4%BA'...'/">table中溢出文本显示&#39;...&#39;</a>
          </li>
        
          <li>
            <a href="/2020/08/03/vue-router%E9%BB%98%E8%AE%A4%E6%98%BE%E7%A4%BA%E5%AD%90%E8%B7%AF%E7%94%B1/">vue-router默认显示子路由</a>
          </li>
        
          <li>
            <a href="/2020/07/20/Markdown%E5%B8%B8%E7%94%A8%E8%AF%AD%E6%B3%95%E7%AE%80%E4%BB%8B/">Markdown常用语法简介</a>
          </li>
        
          <li>
            <a href="/2020/07/18/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>